package ru.averta.notes.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ru.averta.notes.dao.UserDao;
import ru.averta.notes.domain.User;
import ru.averta.notes.exceptions.UserAlreadyExistsException;
import ru.averta.notes.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;

	public void createUser(User user) throws UserAlreadyExistsException {

		User testUser = findUserByMail(user.getMail());
		if (testUser != null) {
			throw new UserAlreadyExistsException("User " + user.getMail() + " already exists");
		}
		
		userDao.create(user);
	}
	
	@Transactional(readOnly = true)
	public User findUserByMail(String mail) {
		return userDao.findUserByMail(mail.toLowerCase());
	}

}
